import javax.swing.JOptionPane;
import java.rmi.Naming;
import java.io.*;
import java.util.*;
import java.util.Vector;
import java.util.Properties;



public class Principal extends javax.swing.JFrame {

    // <editor-fold defaultstate="collapsed" desc="Variables">

    private String ip1;
    private String ip2;
    private String ip3;
    private int port1;
    private int port2;
    private int port3;
    private String url1;
    private String url2;
    private String url3;
    private ITanqueRemoto obj = null;
    private Vector tanques;
    
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructor">

    /**
     * Constructor que inicializa los elementos de url a travez del archivo de
     * propiedades
     */
    public Principal() {

        initComponents();

        Properties props = new Properties();

        try
        {
            props.load(new FileInputStream("ClienteTanque.properties"));

            ip1 = props.getProperty("ip1");
            ip2 = props.getProperty("ip2");
            ip3 = props.getProperty("ip3");
            port1 = Integer.parseInt(props.getProperty("port1"));
            port2 = Integer.parseInt(props.getProperty("port2"));
            port3 = Integer.parseInt(props.getProperty("port3"));
           
        }catch (Exception e)
        {
            System.out.println("Error al tratar de cargar los elementos de los archivos"
                    + " de propiedades");
        }         
        
    }

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Generated Code">

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TanqueLlenar = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        litrosLlenar = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        OK = new javax.swing.JButton();
        ComboTanque = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Envio de Litros");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setText("Sistema de Tanques Petroleros");

        jLabel2.setText("Indique el tanque a llenar:");

        jLabel3.setText("Indique los litros:");

        litrosLlenar.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        jLabel4.setText("Lts");

        OK.setText("Enviar Litros");
        OK.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                OKMouseClicked(evt);
            }
        });

        ComboTanque.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione", "1", "2", "3" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(litrosLlenar, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel4))
                                    .addComponent(ComboTanque, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(148, 148, 148)
                        .addComponent(OK))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(ComboTanque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(litrosLlenar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(39, 39, 39)
                .addComponent(OK)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Evento del Boton">

    private void OKMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_OKMouseClicked

       /*       Estas Variables son para la construccion de las url a las q se les enviara
        *       el xml generado, se pide tanque y litros a llenar, con esto se construye
        *       el xml y se envia la direccion del mismo
        */
       Object tanque;
       String tanquellenar="";
       String litros="";       

       try {

           /*
            se crean los vectores, que tendran el contenido con el q se generara el xml
            */
           TanquesLlenar datos=new TanquesLlenar();
           Vector vectordatos2 = new Vector();


           tanque= ComboTanque.getSelectedItem();
           tanquellenar = tanque.toString();


           vectordatos2.addElement(datos);


           datos.setid(tanquellenar);
           datos.setlts(litrosLlenar.getText());

            url1 = "//" + ip1 + ":" + port1 + "/Tanque1";
            url2 = "//" + ip2 + ":" + port2 + "/Tanque2";
            url3 = "//" + ip3 + ":" + port3 + "/Tanque3";
             
           /*se verifica a donde sera enviado el xml si es tanque 1, 2 o 3*/
            if (tanquellenar.compareTo("1")==0 ){

                obj = (ITanqueRemoto) Naming.lookup(url1);
                obj.EscribirTanquesXml(vectordatos2);

           
            }else if (tanquellenar.compareTo("2")==0){

                obj = (ITanqueRemoto) Naming.lookup(url2);
                obj.EscribirTanquesXml(vectordatos2);
                   
            }else if (tanquellenar.compareTo("3")==0){

                obj = (ITanqueRemoto) Naming.lookup(url3);
                obj.EscribirTanquesXml(vectordatos2);

            }

            litrosLlenar.setText("");
            
        }
        catch (Exception e) {
            e.printStackTrace();
        
        }

    }//GEN-LAST:event_OKMouseClicked

    // </editor-fold>

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ComboTanque;
    private javax.swing.JButton OK;
    private javax.swing.ButtonGroup TanqueLlenar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField litrosLlenar;
    // End of variables declaration//GEN-END:variables

}
